package com.nuttysoft.nutand.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nuttysoft.nutand.utils.ViewUtil;
import com.nuttysoft.nutand.views.TopBar;

/**
 * Created by Administrator on 2016/3/10 0010.
 */
public class NaBaseActivity extends FragmentActivity {

    private TopBar topBar;

    //我们填充的布局
    private LinearLayout containerLayout;

    private LinearLayout parentLayout;

    private LinearLayout loadingLayout;

    private LinearLayout noDataLayout;

    private ProgressBar loadingImg;

    private TextView nodataDescribe;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //初始化内部布局
        //整个父布局
        parentLayout = new LinearLayout(this);
        parentLayout.setOrientation(LinearLayout.VERTICAL);
        //自定义的填充布局
        containerLayout = new LinearLayout(this);
        noDataLayout = new LinearLayout(this);
        loadingLayout = new LinearLayout(this);
        loadingImg = new ProgressBar(this,null,android.R.attr.progressBarStyle);
        nodataDescribe = new TextView(this);
        nodataDescribe.setGravity(Gravity.CENTER);
        nodataDescribe.setTextColor(Color.parseColor("#989898"));
        nodataDescribe.setTextSize(20);
        loadingImg.setVisibility(View.GONE);
        nodataDescribe.setVisibility(View.GONE);
        //顶部栏
        topBar = new TopBar(this);

        //定义参数
        LinearLayout.LayoutParams parentParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout.LayoutParams loadingParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        loadingParams.gravity = Gravity.CENTER;
        LinearLayout.LayoutParams nodataParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        nodataParams.gravity = Gravity.CENTER;
        LinearLayout.LayoutParams topbarParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) ViewUtil.dip2px(this,52));
        parentLayout.addView(topBar,topbarParams);

        parentLayout.addView(loadingImg,loadingParams);
        parentLayout.addView(nodataDescribe,nodataParams);

        parentLayout.addView(containerLayout,containerParams);

        setContentView(parentLayout,parentParams);
    }

    /**
     * 获取topbar
     * */
    public TopBar getTopBar(){
        return topBar;
    }

    public void setNaContentView(int layoutId){
        containerLayout.removeAllViews();
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layoutId,null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        containerLayout.addView(view,params);
    }

    public void showLoadingView(){
        loadingImg.setVisibility(View.VISIBLE);
    }

    public void dissmissLoadingView(){
        loadingImg.setVisibility(View.GONE);
    }

    public void setNodataDescribe(String dex){
        nodataDescribe.setText(dex);
        nodataDescribe.setVisibility(View.VISIBLE);
    }

    public void setNodataDescribe(String dex, View.OnClickListener listener){
        setNodataDescribe(dex);
        nodataDescribe.setOnClickListener(listener);
    }

    public void dismissNodata(){
        nodataDescribe.setVisibility(View.GONE);
    }
}
